package com.fhj.movie.baidumap;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.fhj.movie_fhj_01.R;


public class JyjMapActivity extends Activity {

	private MapView map;
	private BaiduMap mapmanager;
	private PoiSearch poiSearch;
	
	private  LocationClient mLocationClient = null;//初始化LocationClient类
	public  BDLocationListener mylisListener = new MyLocationListener();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		SDKInitializer.initialize(getApplicationContext());
		
		setContentView(R.layout.activity_jyj_map);
		
		mLocationClient = new LocationClient(getApplicationContext());
		
		LocationClientOption option = new LocationClientOption();
		
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
//		option.setScanSpan(5000);
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	
		mLocationClient.registerLocationListener(mylisListener);
		mLocationClient.start();
		
		map = (MapView)this.findViewById(R.id.baidu_mapView);
		mapmanager = map.getMap();
		
		mapmanager.setTrafficEnabled(true);
		mapmanager.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		mapmanager.setMaxAndMinZoomLevel(19, 3);
		
		setPointListener();
		setInterestPoint();
		
		
	}
	
	
	private void setPointListener() {
		// TODO 设置标点监听器
		mapmanager.setOnMarkerClickListener(new OnMarkerClickListener() {
			
			@Override
			public boolean onMarkerClick(Marker arg0) {
				// TODO 自动生成的方法存根
				Button  btn = new Button(getApplicationContext());
				btn.setBackgroundColor(Color.GRAY);
				btn.setText(arg0.getTitle());
				BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromView(btn);
				InfoWindow infoWindow = new InfoWindow(bitmapDescriptor, arg0.getPosition(), -50, new OnInfoWindowClickListener() {
					
					@Override
					public void onInfoWindowClick() {
						// TODO 自动生成的方法存根
						mapmanager.hideInfoWindow();
					}
				});
				
				mapmanager.showInfoWindow(infoWindow);
				return false;
			}
		});
	}
	private void setInterestPoint() {
		//TODO  设置POI检索
		poiSearch = PoiSearch.newInstance();
		poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
			
			@Override
			public void onGetPoiResult(PoiResult arg0) {
				// TODO 自动生成的方法存根
				MyPoiOverlay poiOverlay = new MyPoiOverlay(mapmanager);
				mapmanager.setOnMarkerClickListener(poiOverlay);
				poiOverlay.setData(arg0);
				poiOverlay.addToMap();
			}
			
			@Override
			public void onGetPoiDetailResult(PoiDetailResult arg0) {
				// TODO 自动生成的方法存根
				
			}
		});
	}

	private class MyPoiOverlay extends PoiOverlay{

		public MyPoiOverlay(BaiduMap arg0) {
			super(arg0);
			// TODO 自动生成的构造函数存根
		}
		@Override
		public boolean onPoiClick(int position) {
			// TODO 自动生成的方法存根
			
			List<PoiInfo> list = getPoiResult().getAllPoi();
			final PoiInfo poiInfo = list.get(position);
			
			Button  btn = new Button(getApplicationContext());
			btn.setBackgroundColor(Color.BLUE);
			btn.setText(poiInfo.name);
			
			BitmapDescriptor descriptor = BitmapDescriptorFactory.fromView(btn);
			InfoWindow infoWindow = new InfoWindow(descriptor, poiInfo.location, -50,
					new OnInfoWindowClickListener() {
				
				@Override
				public void onInfoWindowClick() {
					// TODO 自动生成的方法存根
					poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUid(poiInfo.uid));
					mapmanager.hideInfoWindow();
				}
			});
			mapmanager.showInfoWindow(infoWindow);
			
			return super.onPoiClick(position);
		}
	}
	
	
	public class MyLocationListener implements BDLocationListener{

		@Override
		public void onReceiveLocation(BDLocation location) {
			// TODO 自动生成的方法存根
			if (location==null) {
				return ;
			}
			
			Toast.makeText(JyjMapActivity.this, ""+location.getLocType()+","+location.getLatitude()+","+location.getLongitude()+","+location.getAddrStr(), Toast.LENGTH_SHORT).show();
		
			//定位的中心点坐标
			LatLng point= new LatLng(location.getLatitude(), location.getLongitude());
			
			
			BitmapDescriptor bitmap= BitmapDescriptorFactory.fromResource(R.drawable.da_marker_red);
			OverlayOptions options = new MarkerOptions().position(point).icon(bitmap).title(location.getAddrStr());
			mapmanager.addOverlay(options);
			
			locationMyLocation(point);
			
		}

	}
	
	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		map.onResume();
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO 自动生成的方法存根
		map.onPause();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		// TODO 自动生成的方法存根
		map.onDestroy();
		mLocationClient.stop();
		mLocationClient.unRegisterLocationListener(mylisListener);
		
		poiSearch.destroy();
		
		super.onDestroy();
	}
	
	/**
	 * 定位到自己的位置
	 * @param point
	 */
	public  void locationMyLocation(LatLng point) {
		/*
		 * 获取到定位  将定位调整到地图的中心位置
		 */
		
		 //定义地图状态
		MapStatus mMapStatus = new MapStatus.Builder().target(point).zoom(15).build();
		 //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
		MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
		 //改变地图状态
		mapmanager.setMapStatus(mMapStatusUpdate);
		
		//检索兴趣点
//		poiSearch.searchInCity(new PoiCitySearchOption().keyword("电影院").city("北京").pageCapacity(100).pageNum(0));

		poiSearch.searchNearby(new PoiNearbySearchOption().location(point).keyword("电影院").radius(10000));
		
	}
	
}
